﻿using EnglishStudy.Entity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Input;
using Microsoft.Phone.Shell;
using System.Windows.Controls;

namespace EnglishStudy.ViewModel
{
    class VocabularyViewModel : INotifyPropertyChanged
    {
        private LessonDataContext lessonDB;

        // Class constructor, create the data context object.
        public VocabularyViewModel(string lessonDBConnectionString)
        {
            lessonDB = new LessonDataContext(lessonDBConnectionString);
            LoadCollectionsFromDatabase();
        }

        public void LoadCollectionsFromDatabase()
        {
            WordList = getNewWords(true);
        }

        // A list of all new word
        private ObservableCollection<WordTable> _wordList;
        public ObservableCollection<WordTable> WordList
        {
            get { return _wordList; }
            set
            {
                _wordList = value;
                NotifyPropertyChanged("WordList");
            }
        }

        // return a list of word that is in common word
        public ObservableCollection<WordTable> getNewWords(bool islearned)
        {
            var commonWord = (from WordTable wordItem in lessonDB.WordList
                              where wordItem.IsLearned == true
                              select wordItem);
            ObservableCollection<WordTable> commonWordCollection = new ObservableCollection<WordTable>(commonWord);
            if (commonWordCollection != null && commonWordCollection.Count > 0)
                return commonWordCollection;
            else
                return null;
        }

        public void SaveChangesToDB()
        {
            lessonDB.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region Icommand
        public ICommand ItemSelectedCommand
        {
            get
            {
                return new DelegateCommand(ItemSelected, CanExcute);
            }
        }

        private void ItemSelected(object parameter)
        {
            var list = parameter as ListBox;
            if (list.SelectedItem == null)
                return;
            PhoneApplicationService.Current.State["Word"] = list.SelectedItem as WordTable;
            list.SelectedItem = null;
        }

        public bool CanExcute(object obj)
        {
            return true;
        }

        #endregion
    }
}
